﻿using System;

class PrintMatrix
{
    static void Main()
    {
        // get the dimension of matrix
        Console.Write("Enter dimension of the matrix : ");
        string dimensionStr = Console.ReadLine();
        int dimension;
        while (!int.TryParse(dimensionStr, out dimension))
        {
            Console.Write("Invalid input. Enter again dimension for the matrix : ");
            dimensionStr = Console.ReadLine();
        }

        int[,] matrix = new int[dimension, dimension];

        //  1  5  9 13
        //  2  6 10 14
        //  3  7 11 15
        //  4  8 12 16
        /* only printing without saving in a matrix
        for (int i = 1; i <= dimension; i++)
        {
            for (int j = i; j <= dimension*dimension; j += dimension)
            {
                Console.Write("{0, 5}", j);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        Console.WriteLine();
         */

        for (int col = 0; col < dimension; col++)
        {
            for (int row = 0; row < dimension; row++)
            {
                matrix[row, col] = (col * dimension) + row + 1;
            }
        }
        // print the first matrix
        PrintReadyMatrix(matrix);

        //  1  8  9 16
        //  2  7 10 15
        //  3  6 11 14
        //  4  5 12 13      
        for (int col = 0; col < dimension; col++)
        {
            for (int row = 0; row < dimension; row++)
            {
                if (col % 2 == 0)
                {
                    matrix[row, col] = (col * dimension) + row + 1;
                }
                else
                {
                    matrix[row, col] = (col + 1) * dimension - row;
                }
            }
        }
        //print the second matrix
        PrintReadyMatrix(matrix);

        //  7 11 14 16
        //  4  8 12 15
        //  2  5  9 13
        //  1  3  6 10 
        int digit = 0;
        for (int row = dimension - 1; row >= 0; row--)
        {
            for (int col = row; col <= dimension - 1; col++)
            {
                digit++;
                matrix[col, col - row] = digit;
            }
        }
        for (int row = -1; row >= 1 - dimension; row--)
        {
            for (int col = 0; col <= dimension + row - 1; col++)
            {
                digit++;
                matrix[col, col - row] = digit;
            }
        }
        // print the third matrix
        PrintReadyMatrix(matrix);

    }

    static void PrintReadyMatrix(int[,] matrix)
    {
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLongLength(1); col++)
            {
                Console.Write("{0, 5}", matrix[row, col]);
            }
            Console.WriteLine();
        }

        Console.WriteLine();
        Console.WriteLine();
    }
}
